package com.example.seabedmusic.views;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.seabedmusic.R;
import com.example.seabedmusic.adapter.WoDeAdapter;
import com.example.seabedmusic.entity.Playlist;
import com.example.seabedmusic.entity.Song;
import com.example.seabedmusic.utils.PlaylistManager;

import java.util.List;

public class PlaylistDetailActivity extends AppCompatActivity implements WoDeAdapter.OnSongClickListener {
    private RecyclerView rvSongs;
    private TextView tvPlaylistName;
    private TextView tvSongCount;
    private Playlist playlist;
    private PlaylistManager playlistManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playlist_detail);

        initViews();
        loadPlaylistData();
    }

    private void initViews() {
        // 返回按钮
        ImageView ivBack = findViewById(R.id.ivBack);
        ivBack.setOnClickListener(v -> finish());

        tvPlaylistName = findViewById(R.id.tvPlaylistName);
        tvSongCount = findViewById(R.id.tvSongCount);
        rvSongs = findViewById(R.id.rvSongs);
        rvSongs.setLayoutManager(new LinearLayoutManager(this));
    }

    private void loadPlaylistData() {
        String playlistId = getIntent().getStringExtra("playlist_id");
        String playlistName = getIntent().getStringExtra("playlist_name");

        playlistManager = PlaylistManager.getInstance();
        playlist = playlistManager.getPlaylistById(playlistId);

        if (playlist != null) {
            tvPlaylistName.setText(playlist.getName());
            updateSongList();
        } else if (playlistName != null) {
            tvPlaylistName.setText(playlistName);
        }
    }

    private void updateSongList() {
        if (playlist != null) {
            List<Song> songs = playlist.getSongs();
            tvSongCount.setText(songs.size() + " 首歌曲");

            WoDeAdapter adapter = new WoDeAdapter(songs);

            // 设置歌曲点击监听器
            adapter.setOnSongClickListener(this);

            // 设置为非本地音乐模式，让WoDeAdapter使用内置的弹窗逻辑
            adapter.setIsLocalMusic(false);

            rvSongs.setAdapter(adapter);
        }
    }

    /**
     * 点击歌曲 - 跳转到播放页面
     */
    @Override
    public void onSongClick(Song song) {
        // 检查当前歌单是否是"我喜欢的"
        boolean isFavoritePlaylist = playlist != null && "我喜欢的".equals(playlist.getName());

        // 点击歌曲跳转到播放页面
        Intent intent = new Intent(this, MusicPlayerActivity.class);
        intent.putExtra("song_title", song.getTitle());
        intent.putExtra("song_artist", song.getArtist());
        intent.putExtra("song_url", song.getUrl());
        intent.putExtra("song_cover", song.getCover());
        intent.putExtra("song_album", song.getAlbum());
        intent.putExtra("song_lyrics", song.getLyrics());

        // 如果是"我喜欢的"歌单，传递喜欢状态为true
        if (isFavoritePlaylist) {
            intent.putExtra("is_favorite", true);
            android.util.Log.d("PlaylistDetailActivity", "从我喜欢歌单跳转，设置喜欢状态为true");
        } else {
            // 其他歌单，检查歌曲是否在"我喜欢的"中
            boolean isFavorite = playlistManager.isFavorite(song);
            intent.putExtra("is_favorite", isFavorite);
            android.util.Log.d("PlaylistDetailActivity", "从普通歌单跳转，歌曲喜欢状态: " + isFavorite);
        }

        startActivity(intent);
    }

    /**
     * 更多按钮点击 - 空实现，避免重复弹窗
     * WoDeAdapter内部已经处理了弹窗逻辑
     */
    @Override
    public void onMoreClick(Song song) {
        // 这里不做任何操作，让WoDeAdapter完全控制弹窗逻辑
        // 只用于日志记录等非UI操作
        android.util.Log.d("PlaylistDetailActivity", "更多按钮点击歌曲: " + song.getTitle());
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 刷新歌曲列表
        if (playlist != null) {
            playlist = playlistManager.getPlaylistById(playlist.getId());
            updateSongList();
        }
    }
}